/*go*/
var gos=new HashMap();

function findComp(targetCompClass,comps){
	if(!comps){
		return null;
	}
	// print(comps);
	var len=comps.size();
	var comp=null;
	for(var i=len-1;i>=0;i--){
		comp=comps[i];
		if(comp==targetCompClass || comp.isInstanceOf(targetCompClass)){
			return comp;
		}
	}
	return null;
}

var Go_ = createObj(null,"Go_");
Go_.extend({
	getDid:function(){
		this.data.getString(FieldConsts.GoDid,null);
	},
	addComp:function(compClass){
		this.comps.add(compClass);
	},
	findComp:function(targetCompClass){
		var comp=findComp(targetCompClass,this.comps);
		if(!comp){
			// comp=findComp(targetCompClass,this.def.comps);
		}
		if(!comp && this.parent){
			comp=findComp(targetCompClass,this.parent.comps);
		}
		return comp;
	},
	getComps:function(){
		// TODO 剔除被覆盖组件
	}
});

function newGoClass(prefix,parent){
	var c=createObj(parent);
	c.parent=parent;
	c.prefix=prefix;
	c.comps=new ArrayList();
	gos.put(prefix,new HashMap());
	return c;
}

var Go = newGoClass("g",Go_);
var Agent = newGoClass("a",Go);
var UserAccount = newGoClass("ua",Go);
var User = newGoClass("u",Agent);
var Monster = newGoClass("m",Agent);
var Npc = newGoClass("n",Agent);
var Item = newGoClass("i",Go);
var Scene = newGoClass("s",Go);

function newGoId(prefix){// TODO
	var hashmap=gos.get(prefix);
	return prefix+(hashmap.size()+1);
}

function newGoObject(goClass,def){
	var go=createObj(goClass);
	go.parent=goClass;
	go.comps=new ArrayList();
	go.id=newGoId(goClass.prefix);
	go.def=def;
	var data=Data.createConcurrentData();
	for each(var key in def.keys()){
		data.put(key,def.get(key));
	}
	go.data=data;
	var hashmap=gos.get(goClass.prefix);
	hashmap.put(go.id,go);
	return go;
}

/* comp */
var Comp = createObj(null,"Comp");

function createComp(parent,name){
	parent = parent || Comp;
	return createObj(parent,name);
}